package app.base.systemmainframe;

import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import app.base.common.FileOperation;
import app.base.common.UserSessionInfo;
import app.base.common.Utility;

public class SysMainFrameHandle extends HttpServlet{

	private static final long serialVersionUID = 1L;
		
	private String webappServletContextRealpath = null;
		
	private final static String APP_SYS_PROGRAM_LOOKUP_ROOT_PATH_REPLACEMENT = "\\{@WEBAPP_ROOT_PATH\\}";
	private final static String APP_SYS_PROGRAM_LOOKUP_ROOT_PATH = "";
	
	static {

	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		//getting the user's sessions.
	 
		if (webappServletContextRealpath == null)
		{
			webappServletContextRealpath = this.getServletConfig().getServletContext().getRealPath("/");
			//webappContextRealpath = request.getContextPath() + "/";			
		}
		
		out.println(getDefaultHtmlPage(request).toString());

		out.flush();
	} 
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	} 
	
	private StringBuffer getDefaultHtmlPage(HttpServletRequest request)
	{
		StringBuffer pageContent = new StringBuffer();

		pageContent.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
		pageContent.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\">\n");
		pageContent.append("<head>\n");
		pageContent.append("<title>" + Utility.SYSTEM_LOGO_TITLE + " -- " + ((UserSessionInfo) request.getSession().getAttribute(Utility.LOGIN_USER_SESSION_INFO)).getLoginId() + "</title>\n\n");
		pageContent.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
		pageContent.append("<meta content=\"JAVA\" name=\"CODE_LANGUAGE\">\n");
		pageContent.append("<meta content=\"JavaScript\" name=\"DEFAULTCLIENTSCRIPT_LANGUAGE\">\n\n");

		try {
			FileOperation fo = new FileOperation(webappServletContextRealpath + Utility.APP_SYS_MODULES_CONFIGURATION_INCLUDE_PATH);
						
			Pattern pattern = Pattern.compile(APP_SYS_PROGRAM_LOOKUP_ROOT_PATH_REPLACEMENT);
			Matcher matcher = pattern.matcher(fo.readfrom(Utility.CHARSET_ENCODING_UTF8).toString());
			
			pageContent.append(matcher.replaceAll(APP_SYS_PROGRAM_LOOKUP_ROOT_PATH));			
		}
		catch (Exception e)
		{
			pageContent.append(e.toString());
		}
		
		pageContent.append("\n</head>\n");
		pageContent.append("<body>\n");
		pageContent.append("</body>\n");
		pageContent.append("</html>");
		
		return pageContent;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub


		//SysMainFrame smf = new SysMainFrame();
		//System.out.println(smf.getDefaultMainFrameHtmlPageContent());
		 

	}

}